package com.clkj.nangang.mvp.index.communityservice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.clkj.nangang.R;
import com.clkj.nangang.app.CustomApplication;
import com.clkj.nangang.common.Constant;
import com.clkj.nangang.mvp.adapter.NewsListAdapter;
import com.clkj.nangang.mvp.base.BaseActivity;
import com.clkj.nangang.mvp.index.newsdetail.NewsDetailActivity;
import com.clkj.nangang.mvp.model.NewsModel;
import com.clkj.nangang.util.XListView;
import com.clkj.nangang.util.schedulers.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

public class CommunityServiceActivity extends BaseActivity implements CommunityContract.View, XListView.IXListViewListener {

    private XListView lvCommunity;

    private CommunityContract.Presenter presenter;

    private int page = 1;

    private String categoryName;

    private NewsListAdapter newsListAdapter;
    private List<NewsModel> newsModelList = new ArrayList<>();
    private RelativeLayout noticeEmpty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community_service);
        initTitle(null, null, "党务居务一览", true, null);

        initData();
        initView();
        assignView();

        //获取数据
        onRefresh();
    }

    @Override
    public void initData() {

        categoryName = "居务公开";
        presenter = new CommunityPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    @Override
    public void assignView() {

        lvCommunity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String newsId = newsModelList.get(position - 1).getID();

                Intent it = new Intent(CommunityServiceActivity.this, NewsDetailActivity.class);
                it.putExtra("newsId", newsId);
                startActivity(it);

            }
        });
    }

    @Override
    public void initView() {

        lvCommunity = (XListView) findViewById(R.id.lv_community);
        noticeEmpty = (RelativeLayout) findViewById(R.id.notice_empty);

        lvCommunity.setPullLoadEnable(true);
        lvCommunity.setPullRefreshEnable(true);
        lvCommunity.setXListViewListener(this);

        newsListAdapter = new NewsListAdapter(this, newsModelList);
        lvCommunity.setAdapter(newsListAdapter);


    }

    @Override
    public void onNetError(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getCommunityListsSuccess(List<NewsModel> newsModels) {
        if (page == 1) {
            newsModelList.clear();
        }
        lvCommunity.setVisibility(View.VISIBLE);
        if (newsModels.size() > 0) {
            if (newsModels.size() < Constant.DEFAULT_PAGE_SIZE) {
                Toast.makeText(CommunityServiceActivity.this, "数据已经全部加载", Toast.LENGTH_SHORT).show();
                lvCommunity.setPullLoadEnable(false);
            } else {
                lvCommunity.setPullLoadEnable(true);
            }
            newsModelList.addAll(newsModels);
        } else {
            lvCommunity.setPullLoadEnable(false);
            if (newsModelList.size() == 0) {
                lvCommunity.setEmptyView(noticeEmpty);
            } else {
                Toast.makeText(CommunityServiceActivity.this, "数据已经全部加载", Toast.LENGTH_SHORT).show();
            }
        }
        //停止刷新或加载
        onStopLoad();

        newsListAdapter.notifyDataSetChanged();
    }

    @Override
    public void getCommunityListsFail(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(CommunityContract.Presenter presenter) {
        this.presenter = presenter;
    }


    //刷新和加载
    @Override
    public void onRefresh() {
        page = 1;
        presenter.getCommunityLists(categoryName, String.valueOf(page));
    }

    @Override
    public void onLoadMore() {
        page++;
        presenter.getCommunityLists(categoryName, String.valueOf(page));
    }

    private void onStopLoad() {
        lvCommunity.stopRefresh();
        lvCommunity.stopLoadMore();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unsubscribe();
    }
}
